package scripts.java.code;

import com.dean.dynamicexe.service.ComputerScriptService;
import com.dean.entity.DeanResponse;
import com.dean.entity.TableField;
import com.dean.entity.TableInfo;
import com.dean.utils.DeanEntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class EntityCode implements ComputerScriptService {
    private final static Logger log = LoggerFactory.getLogger(EntityCode.class);
    public DeanResponse<String> call(Map<String, ?> params) {
        String filePath  = params.get("filePath").toString();
        String packageName =  params.get("packageName").toString();
        TableInfo tableInfo = (TableInfo)params.get("tableInfo");
        List<TableField> tableFields = (List<TableField>)params.get("tableFields");
        List<TableField> pkFields =tableFields.stream().filter(tableField -> "Y".equals(tableField.getPkFlag())).collect(Collectors.toList());
        List<String> importCLasses = DeanEntityUtils.parseImportClasses(tableFields);
        String packagePath = packageName.replace(".", File.separator);
        filePath = filePath + packagePath;
        try(FileWriter writer = new FileWriter(filePath)){
            // package
            writer.append("package " ).append(packageName).append(";").append("\r\n");
            // import
            writer.append("import java.io.Serializable;").append("\r\n")
                    .append("import java.util.Objects;").append("\r\n")
                    .append("import jakarta.persistence.Column;").append("\r\n")
                    .append("jakarta.persistence.Entity;").append("\r\n")
                    .append("import jakarta.persistence.Id;").append("\r\n")
                    .append("import jakarta.persistence.Table;").append("\r\n");
            if(pkFields.size() > 1){
                writer.append("import jakarta.persistence.IdClass;");
            }
            for (String importCLass : importCLasses) {
                writer.append("import ").append(importCLass).append(";").append("\r\n");
            }
            // 注解
            writer.append("@Entity").append("\r\n")
                    .append("@Table(name = \"").append(tableInfo.getTableName().toLowerCase()).append("\")").append("\r\n");

            // class
            writer.append("public class ").append(tableInfo.getFirstUpperEntityName()).append(" implements Serializable{").append("\r\n")
                    .append("private static final long serialVersionUID = 1L;").append("\r\n");

            for (TableField tableField : tableFields) {
                writer.append("/**").append("\r\n")
                        .append("*").append("*/").append("\r\n");

                if("Y".equals(tableField.getPkFlag())){
                    writer.append("@Id").append("\r\n");
                }
                writer.append("@Column(name = \"").append(tableField.getColumnName()).append("\")").append("\r\n")
                        .append("private ").append(tableField.getFieldType()).append(tableField.getFieldName()).append(";\r\n");
            }

        }catch (Exception e){
            log.error(e.getMessage(),e);
            return DeanResponse.error(996,e.getMessage());
        }

        return DeanResponse.ok();
    }
};